package org.jeecg.modules.friends.handler;

import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.NIANKA_ID;
import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.YUEKA_ID;
import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.ZHOUKA_ID;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.friends.dao.SystemConfigDAO;
import org.jeecg.modules.friends.model.SystemConfigDO;
import org.jeecg.modules.friends.model.TradeOrderDO;
import org.jeecg.modules.friends.service.TradeOrderService;
import org.jeecg.modules.friends.service.VipUserService;
import org.jeecg.modules.friends.trade.dto.RefundSuccessPushDTO;
import org.jeecg.modules.friends.trade.dto.TradeOrderExtData;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cold
 * @data 2024/6/22 23:56
 * @version 1.0
 */
@Component
@Slf4j
public class TradeSysRefundPushHandler extends AbstractTradeOrderHandler {

    @Autowired
    private TradeOrderService tradeOrderService;

    @Override
    public String eventType() {
        return "trade_refund_SysRefund";
    }

    @Override
    public boolean doDispatch(String eventType, String entity) {
        JSONObject msgJson = JSON.parseObject(entity);
        String msgDecode = msgJson.getString("msg");
        String orderJsonString = null;
        try {
            orderJsonString = URLDecoder.decode(msgDecode, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        log.info("收到退货成功消息，msg:{}", orderJsonString);
        RefundSuccessPushDTO refundSuccessPushDTO = JSON.parseObject(orderJsonString, RefundSuccessPushDTO.class);
        if (StringUtils.isNotEmpty(refundSuccessPushDTO.getOids())) {
            tradeOrderService.refundOrder(refundSuccessPushDTO.getOids());
        }
        return false;
    }
}
